#include "framelessmdimainwindow.h"
#include "framelessmdichildwindow.h"
#include <QTabBar>
#include <QDockWidget>
FramelessMDIMainWindow::FramelessMDIMainWindow()
{
    initTitleRibbonBar();
    initTreeWidget();
    initTabWidget();
    initSplitter();
    initStatusBar();
    loadSkin();
}
void FramelessMDIMainWindow::dealSkinChange(QString skinFile)
{
    loadSkin(skinFile);
}
void FramelessMDIMainWindow::loadSkin(const QString &qssFile)
{
    if(!qssFile.isEmpty())
    {
        QFile file(qssFile);
        if(file.open(QFile::ReadOnly | QFile::Text))
        {
            QTextStream stream(&file);
            setStyleSheet(stream.readAll());

            for(QWidget *w : listChildWindow)
            {
                if(w)
                {
                    w->setStyleSheet(stream.readAll());
                }
            }
            file.close();

            skinQssFile = qssFile;
        }

    }
}
void FramelessMDIMainWindow::initTitleRibbonBar()
{
    titleRibbonBar = new TitleRibbonBar(TitleBarElementType::ICON |
                                        TitleBarElementType::TITLE |
                                        TitleBarElementType::SKIN |
                                        TitleBarElementType::MIN |
                                        TitleBarElementType::MAX |
                                        TitleBarElementType::RESTORE |
                                        TitleBarElementType::CLOSE,
                                        this);
    titleRibbonBar->setTitleBarElement(TitleBarElementType::TITLE , "标题栏");
    //第一个ribbontoolbar
    RibbonToolBar* tb1 = new RibbonToolBar("预报");
    titleRibbonBar->addRibbonToolBar(tb1);
        //1-1
    RibbonToolButtonGroup* gp11 = new RibbonToolButtonGroup();
    titleRibbonBar->addRibbonToolButtonGroup(tb1, gp11);
            //1-1-1
    RibbonAction* act111 = new RibbonAction("洪水预报","洪水预报",QIcon(":/pic/toolbuttonIcon/new_file.png"));
    RibbonToolButton* bt111 = new RibbonToolButton;
    bt111->setDefaultAction(act111);
    connect(act111, &RibbonAction::triggered, this, [](){qDebug()<<"Clicked!";});
    titleRibbonBar->addRibbonToolButton(tb1, gp11, bt111);

        //1-2
    RibbonToolButtonGroup* gp12 = new RibbonToolButtonGroup();
    titleRibbonBar->addRibbonToolButtonGroup(tb1, gp12);
            //1-2-1
    RibbonAction* act121 = new RibbonAction("洪水预报","洪水预报",QIcon(":/pic/toolbuttonIcon/map.png"));
    RibbonToolButton* bt121 = new RibbonToolButton;
    bt121->setDefaultAction(act121);
    connect(act121, &RibbonAction::triggered, this, [](){qDebug()<<"Clicked!";});
    tb1->setGroupEnd();
    titleRibbonBar->addRibbonToolButton(tb1, gp12, bt121);

    //第2个ribbontoolbar
    RibbonToolBar* tb2 = new RibbonToolBar("预报2");
    titleRibbonBar->addRibbonToolBar(tb2);
        //2-1
    RibbonToolButtonGroup* gp21 = new RibbonToolButtonGroup();
    titleRibbonBar->addRibbonToolButtonGroup(tb2, gp21);
            //2-1-1
    RibbonAction* act211 = new RibbonAction("洪水预报","洪水预报",QIcon(":/pic/toolbuttonIcon/search.png"));
    RibbonToolButton* bt211 = new RibbonToolButton;
    bt211->setDefaultAction(act211);
    connect(act211, &RibbonAction::triggered, this, [](){qDebug()<<"Clicked!";});
    tb2->setGroupEnd();
    titleRibbonBar->addRibbonToolButton(tb2, gp21, bt211);


    //第3个ribbontoolbar
    RibbonToolBar* tb3 = new RibbonToolBar("视图");
    titleRibbonBar->addRibbonToolBar(tb3);
        //3-1
    RibbonToolButtonGroup* gp31 = new RibbonToolButtonGroup();
    titleRibbonBar->addRibbonToolButtonGroup(tb3, gp31);
            //3-1-1
    RibbonAction* act311 = new RibbonAction("新建","新建视图",QIcon(":/pic/toolbuttonIcon/new_file.png"));
    RibbonToolButton* bt311 = new RibbonToolButton;
    bt311->setDefaultAction(act311);
    connect(act311, &RibbonAction::triggered, this,
            [=]()
            {
                /*myMDIChildWidget * sub = new myMDIChildWidget;
                addSubWindow(sub);
                sub->show();*/
            });
    titleRibbonBar->addRibbonToolButton(tb3, gp31, bt311);



        //3-2
    RibbonToolButtonGroup* gp32 = new RibbonToolButtonGroup();
    titleRibbonBar->addRibbonToolButtonGroup(tb3, gp32);
            //3-2-1
    RibbonAction* act321 = new RibbonAction("堆叠视图","堆叠视图",QIcon(":/pic/toolbuttonIcon/cascade.png"));
    RibbonToolButton* bt321 = new RibbonToolButton;
    bt321->setDefaultAction(act321);
    connect(act321, &RibbonAction::triggered, this,
        [=]()
        {
            cascadeSubWindows();
        });
    titleRibbonBar->addRibbonToolButton(tb3, gp32, bt321);


            //3-2-2
    RibbonAction* act322 = new RibbonAction("平铺视图","平铺视图",QIcon(":/pic/toolbuttonIcon/tile.png"));
    RibbonToolButton* bt322 = new RibbonToolButton;
    bt322->setDefaultAction(act322);
    connect(act322, &RibbonAction::triggered, this,
        [=]()
        {
           tileSubWindows();
        });
    titleRibbonBar->addRibbonToolButton(tb3, gp32, bt322);
    tb3->setGroupEnd();

    addWidget(titleRibbonBar, 0 ,Qt::AlignTop);



}

void FramelessMDIMainWindow::initSplitter()
{
    hSplitter = new QSplitter(Qt::Horizontal, this);

    if(treeWidget)
        hSplitter->addWidget(treeWidget);

    if(tabWidget)
        hSplitter->addWidget(tabWidget);

    if(listWidget)
        hSplitter->addWidget(listWidget);

    hSplitter->setStretchFactor(0, 0);
    hSplitter->setStretchFactor(1, 1);
    hSplitter->setStretchFactor(2, 0);

    addWidget(hSplitter, 1);
}

void FramelessMDIMainWindow::initTabWidget()
{
    tabWidget = new QTabWidget;
    tabWidget->setObjectName("tabWidget");
    tabWidget->setTabsClosable(true);


    connect(tabWidget, &QTabWidget::tabCloseRequested, this,
    [&](int index)
    {
        tabWidget->removeTab(index);
    });
    tabWidget->tabBar()->setObjectName("tabWidgetTabBar");


    tabWidget->addTab(new framelessChildWidget, "sdf");
    tabWidget->addTab(new framelessChildWidget, "sdf");
    //tabWidget->tabBar()->setTabButton(2, QTabBar::RightSide, new QPushButton("+"));

}
void FramelessMDIMainWindow::initTreeWidget()
{
    treeWidget = new titleTreeWidget;
    listWidget = new titleListWidget;
/*    QTreeWidget *treeWidget = new QTreeWidget;
    treeWidget->setObjectName("treeWidget");
    treeWidget->setMinimumWidth(200);
    treeWidget->setMaximumWidth(330);
    treeWidget->setColumnWidth(0,160);
    treeWidget->setFont(QFont("宋体",11));
    treeWidget->setColumnCount(2);
    treeWidget->setHeaderLabels(QStringList()<<"项"<<"值");

    QTreeWidgetItem * itemRoot = new QTreeWidgetItem(QStringList()<<"工程方案");
    treeWidget->addTopLevelItem(itemRoot);
    treeWidget->setRootIsDecorated(true);

    QTreeWidgetItem * itemChild1 = new QTreeWidgetItem(QStringList()<<"方案1");
    itemRoot->addChild(itemChild1);
    itemChild1->setExpanded(true);

    QTreeWidgetItem * itemChild11 = new QTreeWidgetItem(QStringList()<<"方案11");
    itemChild1->addChild(itemChild11);

    QTreeWidgetItem * itemChild2 = new QTreeWidgetItem(QStringList()<<"方案2");
    itemRoot->addChild(itemChild2);
    itemChild2->setExpanded(true);


    dockTreeWidget = new QDockWidget;
    dockTreeWidget->setWidget(treeWidget);*/
}
void FramelessMDIMainWindow::initStatusBar()
{
    statusBar = new QStatusBar;
    statusBar->addWidget(new QLabel("  版本:V1.0  "));
    statusBar->addWidget(new QLabel("sdfsfef"));
    statusBar->addWidget(new QLabel("sdfsfef"));

    statusBar->addPermanentWidget(new QLabel("  ©单位名称  "));
    addWidget(statusBar);
}
